/*
 * @Author: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
 * @Date: 2023-05-04 23:14:25
 * @LastEditors: zt 2993721164@qq.com
 * @LastEditTime: 2025-08-06 21:32:58
 * @FilePath: /yyshort-master/lib/pages/frame/sign_in/controller.dart
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:google_sign_in/google_sign_in.dart';
import 'package:yyshort/common/apis/apis.dart';
import 'package:yyshort/common/model/index.dart';
import 'package:yyshort/common/routers/routes.dart';
import 'package:yyshort/common/store/store.dart';
import 'package:yyshort/common/utils/utils.dart';
import 'package:yyshort/common/widgets/widgets.dart';
import 'package:get/get.dart';
import 'package:sign_in_with_apple/sign_in_with_apple.dart';

import 'index.dart';

class SignInController extends GetxController {
  final state = SignInState();

  SignInController();

  // email的控制器
  final TextEditingController emailController = TextEditingController();
  // 密码的控制器
  final TextEditingController passController = TextEditingController();

  // final MyRepository repository;
  // SignInController({@required this.repository}) : assert(repository != null);

  // 跳转 注册界面
  handleNavSignUp() {
    Get.toNamed(AppRoutes.SIGN_UP);
  }

  // 忘记密码
  handleFogotPassword() {
    toastInfo(msg: '忘记密码');
  }

  // 苹果登录
  iosLogin() async {
    // 这里可以添加苹果登录的逻辑
    // 例如调用第三方库进行苹果登录
    toastInfo(msg: '苹果登录功能待实现');
    final credential = await SignInWithApple.getAppleIDCredential(
      scopes: [
        AppleIDAuthorizationScopes.email,
        AppleIDAuthorizationScopes.fullName,
      ],
      nonce: 'example-nonce',
      state: 'example-state',
    );

    // ignore: avoid_print
    // print(credential);
  }

  // 执行登录操作
  handleSignIn() async {
    // if (!duIsEmail(_emailController.value.text)) {
    //   toastInfo(msg: '请正确输入邮件');
    //   return;
    // }
    // if (!duCheckStringLength(_passController.value.text, 6)) {
    //   toastInfo(msg: '密码不能小于6位');
    //   return;
    // }

    UserLoginRequestEntity params = UserLoginRequestEntity(
      email: emailController.value.text,
      password: duSHA256(passController.value.text),
    );

    UserLoginResponseEntity userProfile = await UserAPI.login(params: params);
    UserStore.to.saveProfile(userProfile);
    // 跳转到应用首页
    Get.offAndToNamed(AppRoutes.Application);
  }

  guestLogin() async {
    // 这里可以添加游客登录的逻辑
    // 保存 token
    UserLoginResponseEntity guestUser = await UserAPI.guestLogin();
    await UserStore.to.setToken(guestUser.token ?? '');
    // 跳转到应用首页
    Get.offAndToNamed(AppRoutes.Application);
  }

  @override
  void onReady() {
    super.onReady();
  }

  @override
  void dispose() {
    emailController.dispose();
    passController.dispose();
    super.dispose();
  }
}
